﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace AfterCaveStory
{
    class PhysicsObject : AnimatedObjetc
    {
        public Vector2 velocity;
        public Boolean isStatic;

        
        
        public PhysicsObject(Texture2D textura, Nullable<Rectangle> sourceRectangle, float depth)
            : base(textura, sourceRectangle, depth)
        {
            velocity = Vector2.Zero;
            isStatic = true;

        }

        public virtual void update(World world)
        {
            if (!isStatic)
            {
                verifyGravity(world);
                verifyMoviment(world);
                verifyCollisions(world);
            }
        }

        public void verifyGravity(World world)
        {
            this.velocity.Y += world.gravidade;
        }

        public void verifyMoviment(World world)
        {
            this.x += velocity.X;
            this.y += velocity.Y;
        }

        public void verifyCollisions(World world)
        {
            for (int i = 0; i < world.objectList.Count; i++)
            {
                if (world.objectList[i] is Platform)
                {
                    if (this.collisionBounds.Intersects(world.objectList[i].collisionBounds))
                    {
                        this.velocity.Y = 0;
                        this.y = world.objectList[i].y - world.objectList[i].Height / 2 - this.Height / 2;
                    }
                }
            }
        }
    }
}
